package main

import (
	"context"
	"fmt"
	"github.com/asim/go-micro/plugins/registry/mdns/v3"
	"github.com/asim/go-micro/v3/client"
	"github.com/asim/go-micro/v3/registry"
	user "main/proto"
	"time"
)

const (
	ServerName = "go.micro.registry.mdns"
	ServerMethod = "User.GetUserInfo"
)

func main() {
	// 创建注册中心
	mdnsRegistry := mdns.NewRegistry(
		registry.Timeout(time.Millisecond * 100),
		func(options *registry.Options) {
			options.Context = context.WithValue(
				context.Background(),
				"mdns.domain",
				"micros",
			)
		},
	)

	// 创建客户端
	cli := client.NewClient(
		client.Registry(mdnsRegistry),
	)

	// 创建请求和返回
	param := &user.UserInfoRequest{UserName: "筱怪"}
	req := client.NewRequest(ServerName, ServerMethod, param)
	rsp := &user.UserInfoResponse{}

	// 请求
	if err := cli.Call(context.Background(), req, rsp); err != nil {
		panic(err)
	}
	fmt.Println("call rpc success: ", rsp)
}
